package org.jarraiders.musiclibrary.client.presenter;

import java.util.ArrayList;
import java.util.List;

import org.jarraiders.musiclibrary.client.ArtistsServiceAsync;
import org.jarraiders.musiclibrary.client.event.AddArtistEvent;
import org.jarraiders.musiclibrary.client.event.EditArtistEvent;
import org.jarraiders.musiclibrary.shared.domain.ArtistDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ArtistsPresenter implements Presenter {

	private List<ArtistDTO> artistDetails;

	public interface Display {
		HasClickHandlers getAddButton();

		HasClickHandlers getDeleteButton();

		HasClickHandlers getList();

		HasClickHandlers getSearchButton();

		String getSearchText();

		void setData(List<String> data);

		int getClickedRow(ClickEvent event);

		Integer getSelectedArtist();

		Widget asWidget();
	}

	private final ArtistsServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public ArtistsPresenter(ArtistsServiceAsync rpcService, HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}

	public void bind() {
		display.getAddButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new AddArtistEvent());
			}
		});

		display.getDeleteButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				deleteSelectedArtist();
			}
		});

		display.getList().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0) {
					String id = artistDetails.get(selectedRow).getArtistId();
					eventBus.fireEvent(new EditArtistEvent(id));
				}
			}
		});

		display.getSearchButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				searchArtist();
			}
		});

	}

	@Override
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
		fetchArtistDetails();

	}

	public void sortArtistDetails() {

		for (int i = 0; i < artistDetails.size(); ++i) {
			for (int j = 0; j < artistDetails.size() - 1; ++j) {
				if (artistDetails.get(j).getArtistName().compareToIgnoreCase(artistDetails.get(j + 1).getArtistName()) >= 0) {
					ArtistDTO tmp = artistDetails.get(j);
					artistDetails.set(j, artistDetails.get(j + 1));
					artistDetails.set(j + 1, tmp);
				}
			}
		}
	}

	public void setArtistDetails(List<ArtistDTO> artistDetails) {
		this.artistDetails = artistDetails;
	}

	public ArtistDTO getArtistDetail(int index) {
		return artistDetails.get(index);
	}

	private void fetchArtistDetails() {
		rpcService.getArtistDetails(new AsyncCallback<ArrayList<ArtistDTO>>() {
			public void onSuccess(ArrayList<ArtistDTO> result) {
				artistDetails = result;
				sortArtistDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(artistDetails.get(i).getArtistName());
				}

				display.setData(data);
			}

			public void onFailure(Throwable caught) {
				Window.alert("Error fetching contact details");
			}
		});
	}

	/*private void deleteSelectedArtists() {
		List<Integer> selectedRows = display.getSelectedRows();
		ArrayList<String> ids = new ArrayList<String>();

		for (int i = 0; i < selectedRows.size(); ++i) {
			ids.add(artistDetails.get(selectedRows.get(i)).getArtistId());
		}

		rpcService.deleteArtists(ids, new AsyncCallback<ArrayList<ArtistDTO>>() {
			public void onSuccess(ArrayList<ArtistDTO> result) {
				artistDetails = result;
				sortArtistDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(artistDetails.get(i).getArtistName());
				}

				display.setData(data);

			}

			public void onFailure(Throwable caught) {
				Window.alert("Error deleting selected contacts");
			}
		});
	}
*/
	public void deleteSelectedArtist() {		
				
		rpcService.deleteArtist(artistDetails.get(display.getSelectedArtist()).getArtistId(), new AsyncCallback<ArrayList<ArtistDTO>>() {
			
			@Override
			public void onSuccess(ArrayList<ArtistDTO> result) {
				artistDetails = result;
				sortArtistDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(artistDetails.get(i).getArtistName());
				}

				display.setData(data);				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void searchArtist(){
		rpcService.searchArtist(display.getSearchText(), new AsyncCallback<ArrayList<ArtistDTO>>() {

			@Override
			public void onSuccess(ArrayList<ArtistDTO> result) {
				artistDetails = result;
				sortArtistDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(artistDetails.get(i).getArtistName());
				}

				display.setData(data);

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error searching artist");

			}
		});
	}




}
